﻿// Decompiled with JetBrains decompiler
// Type: XO1.CanvasOperations
// Assembly: XO1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8FC10D3F-5BDE-46BB-8E15-DDC98B50B0AE
// Assembly location: C:\Users\KasimGulcan\Desktop\Kosgeb\DynaCAD\Project\DynaCAD\bin\Debug\XO1.dll

using LineWithArrows;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using XamlEditor;

namespace XO1
{
  public static class CanvasOperations
  {
    public static Canvas PreparedCanvas(Canvas C)
    {
      Canvas canvas = (Canvas) CloneUIElement.cloneElement((UIElement) C);
      List<double> canvasDimensions = CanvasOperations.GetCanvasDimensions(C);
      if (canvasDimensions.Count > 0)
      {
        canvas.Width = canvasDimensions[0] - canvasDimensions[2];
        canvas.Height = canvasDimensions[1] - canvasDimensions[3];
        foreach (UIElement element in Enumerable.ToList<UIElement>(Enumerable.OfType<UIElement>((IEnumerable) canvas.Children)))
        {
          if (element is LineArrow)
          {
            ((LineArrow) element).StartPoint = new Point(((LineArrow) element).StartPoint.X - canvasDimensions[2], ((LineArrow) element).StartPoint.Y - canvasDimensions[3]);
            ((LineArrow) element).EndPoint = new Point(((LineArrow) element).EndPoint.X - canvasDimensions[2], ((LineArrow) element).EndPoint.Y - canvasDimensions[3]);
          }
          else
          {
            if (double.IsNaN(Canvas.GetLeft(element)))
              Canvas.SetLeft(element, -canvasDimensions[2]);
            else
              Canvas.SetLeft(element, Canvas.GetLeft(element) - canvasDimensions[2]);
            if (double.IsNaN(Canvas.GetTop(element)))
              Canvas.SetTop(element, -canvasDimensions[3]);
            else
              Canvas.SetTop(element, Canvas.GetTop(element) - canvasDimensions[3]);
          }
        }
      }
      return canvas;
    }

    private static List<double> GetCanvasDimensions(Canvas C)
    {
      double num1;
      double num2 = num1 = -5000.0;
      double num3;
      double num4 = num3 = 5000.0;
      List<double> list = new List<double>();
      if (C.Children.Count < 0 || C.Children[0] is Canvas)
        return list;
      foreach (UIElement uiElement in Enumerable.ToList<UIElement>(Enumerable.OfType<UIElement>((IEnumerable) C.Children)))
      {
        if (uiElement is Path)
        {
          if (uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).BottomRight.X > num2)
            num2 = uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).BottomRight.X;
          if (uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).BottomRight.Y > num1)
            num1 = uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).BottomRight.Y;
          if (uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).TopLeft.X < num4)
            num4 = uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).TopLeft.X;
          if (uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).TopLeft.Y < num3)
            num3 = uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).TopLeft.Y;
        }
        else if (uiElement is LineArrow)
        {
          Point point1 = new Point(Math.Min(((LineArrow) uiElement).StartPoint.X, ((LineArrow) uiElement).EndPoint.X), Math.Min(((LineArrow) uiElement).StartPoint.Y, ((LineArrow) uiElement).EndPoint.Y));
          Point point2 = new Point(Math.Max(((LineArrow) uiElement).StartPoint.X, ((LineArrow) uiElement).EndPoint.X), Math.Max(((LineArrow) uiElement).StartPoint.Y, ((LineArrow) uiElement).EndPoint.Y));
          if (point2.X > num2)
            num2 = point2.X;
          if (point2.Y > num1)
            num1 = point2.Y;
          if (point1.X < num4)
            num4 = point1.X;
          if (point1.Y < num3)
            num3 = point1.Y;
        }
        else if (uiElement is TextBox)
        {
          if (uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).BottomRight.X > num2)
            num2 = uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).BottomRight.X;
          if (uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).BottomRight.Y > num1)
            num1 = uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).BottomRight.Y;
          if (uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).TopLeft.X < num4)
            num4 = uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).TopLeft.X;
          if (uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).TopLeft.Y < num3)
            num3 = uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).TopLeft.Y;
        }
      }
      if (num2 > num4 && num1 > num3)
      {
        list.Add(num2);
        list.Add(num1);
        list.Add(num4);
        list.Add(num3);
      }
      return list;
    }

    public static Canvas PreparedCanvasAll(Canvas C)
    {
      Canvas canvas = (Canvas) CloneUIElement.cloneElement((UIElement) C);
      List<double> canvasDimensionsAll = CanvasOperations.GetCanvasDimensionsAll(C);
      if (canvasDimensionsAll.Count > 0)
      {
        canvas.Width = canvasDimensionsAll[0] - canvasDimensionsAll[2];
        canvas.Height = canvasDimensionsAll[1] - canvasDimensionsAll[3];
        foreach (UIElement element in Enumerable.ToList<UIElement>(Enumerable.OfType<UIElement>((IEnumerable) canvas.Children)))
        {
          if (element is LineArrow)
          {
            ((LineArrow) element).StartPoint = new Point(((LineArrow) element).StartPoint.X - canvasDimensionsAll[2], ((LineArrow) element).StartPoint.Y - canvasDimensionsAll[3]);
            ((LineArrow) element).EndPoint = new Point(((LineArrow) element).EndPoint.X - canvasDimensionsAll[2], ((LineArrow) element).EndPoint.Y - canvasDimensionsAll[3]);
          }
          else
          {
            if (double.IsNaN(Canvas.GetLeft(element)))
              Canvas.SetLeft(element, -canvasDimensionsAll[2]);
            else
              Canvas.SetLeft(element, Canvas.GetLeft(element) - canvasDimensionsAll[2]);
            if (double.IsNaN(Canvas.GetTop(element)))
              Canvas.SetTop(element, -canvasDimensionsAll[3]);
            else
              Canvas.SetTop(element, Canvas.GetTop(element) - canvasDimensionsAll[3]);
          }
        }
      }
      return canvas;
    }

    private static List<double> GetCanvasDimensionsAll(Canvas C)
    {
      double num1;
      double num2 = num1 = -5000.0;
      double num3;
      double num4 = num3 = 5000.0;
      List<double> list = new List<double>();
      if (C.Children.Count < 0)
        return list;
      if (!(C.Children[0] is Canvas))
      {
        foreach (UIElement uiElement in Enumerable.ToList<UIElement>(Enumerable.OfType<UIElement>((IEnumerable) C.Children)))
        {
          if (uiElement is Path)
          {
            if (uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).BottomRight.X > num2)
              num2 = uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).BottomRight.X;
            if (uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).BottomRight.Y > num1)
              num1 = uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).BottomRight.Y;
            if (uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).TopLeft.X < num4)
              num4 = uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).TopLeft.X;
            if (uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).TopLeft.Y < num3)
              num3 = uiElement.TransformToVisual((Visual) C).TransformBounds(((Path) uiElement).Data.Bounds).TopLeft.Y;
          }
          else if (uiElement is LineArrow)
          {
            Point point1 = new Point(Math.Min(((LineArrow) uiElement).StartPoint.X, ((LineArrow) uiElement).EndPoint.X), Math.Min(((LineArrow) uiElement).StartPoint.Y, ((LineArrow) uiElement).EndPoint.Y));
            Point point2 = new Point(Math.Max(((LineArrow) uiElement).StartPoint.X, ((LineArrow) uiElement).EndPoint.X), Math.Max(((LineArrow) uiElement).StartPoint.Y, ((LineArrow) uiElement).EndPoint.Y));
            if (point2.X > num2)
              num2 = point2.X;
            if (point2.Y > num1)
              num1 = point2.Y;
            if (point1.X < num4)
              num4 = point1.X;
            if (point1.Y < num3)
              num3 = point1.Y;
          }
          else if (uiElement is TextBox)
          {
            if (uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).BottomRight.X > num2)
              num2 = uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).BottomRight.X;
            if (uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).BottomRight.Y > num1)
              num1 = uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).BottomRight.Y;
            if (uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).TopLeft.X < num4)
              num4 = uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).TopLeft.X;
            if (uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).TopLeft.Y < num3)
              num3 = uiElement.TransformToVisual((Visual) C).TransformBounds(new Rect(new Point(0.0, 0.0), new Point(((FrameworkElement) uiElement).Width, ((FrameworkElement) uiElement).Height))).TopLeft.Y;
          }
        }
        if (num2 > num4 && num1 > num3)
        {
          list.Add(num2);
          list.Add(num1);
          list.Add(num4);
          list.Add(num3);
        }
      }
      else
      {
        foreach (Canvas canvas in Enumerable.OfType<Canvas>((IEnumerable) C.Children))
        {
          foreach (UIElement element in Enumerable.ToList<UIElement>(Enumerable.OfType<UIElement>((IEnumerable) canvas.Children)))
          {
            if (element is Path)
            {
              GeneralTransform generalTransform = (GeneralTransform) canvas.RenderTransform;
              if (generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(((Path) element).Data.Bounds)).BottomRight.X > num2)
                num2 = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(((Path) element).Data.Bounds)).BottomRight.X;
              Point point = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(((Path) element).Data.Bounds)).BottomRight;
              if (point.Y > num1)
              {
                point = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(((Path) element).Data.Bounds)).BottomRight;
                num1 = point.Y;
              }
              point = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(((Path) element).Data.Bounds)).TopLeft;
              if (point.X < num4)
              {
                point = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(((Path) element).Data.Bounds)).TopLeft;
                num4 = point.X;
              }
              point = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(((Path) element).Data.Bounds)).TopLeft;
              if (point.Y < num3)
              {
                point = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(((Path) element).Data.Bounds)).TopLeft;
                num3 = point.Y;
              }
            }
            else if (element is LineArrow)
            {
              Point point1 = new Point(Math.Min(((LineArrow) element).StartPoint.X, ((LineArrow) element).EndPoint.X), Math.Min(((LineArrow) element).StartPoint.Y, ((LineArrow) element).EndPoint.Y));
              Point point2 = new Point(Math.Max(((LineArrow) element).StartPoint.X, ((LineArrow) element).EndPoint.X), Math.Max(((LineArrow) element).StartPoint.Y, ((LineArrow) element).EndPoint.Y));
              if (point2.X > num2)
                num2 = point2.X;
              if (point2.Y > num1)
                num1 = point2.Y;
              if (point1.X < num4)
                num4 = point1.X;
              if (point1.Y < num3)
                num3 = point1.Y;
            }
            else if (element is TextBox)
            {
              double num5 = Canvas.GetLeft(element);
              double num6 = Canvas.GetTop(element);
              if (double.IsNaN(num5))
                num5 = 0.0;
              if (double.IsNaN(num6))
                num6 = 0.0;
              GeneralTransform generalTransform = (GeneralTransform) canvas.RenderTransform;
              Point point = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(new Rect(new Point(num5, num6), new Point(((FrameworkElement) element).Width + num5, ((FrameworkElement) element).Height + num6)))).BottomRight;
              Rect rect;
              if (point.X > num2)
              {
                rect = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(new Rect(new Point(num5, num6), new Point(((FrameworkElement) element).Width + num5, ((FrameworkElement) element).Height + num6))));
                point = rect.BottomRight;
                num2 = point.X;
              }
              rect = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(new Rect(new Point(num5, num6), new Point(((FrameworkElement) element).Width + num5, ((FrameworkElement) element).Height + num6))));
              point = rect.BottomRight;
              if (point.Y > num1)
              {
                rect = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(new Rect(new Point(num5, num6), new Point(((FrameworkElement) element).Width + num5, ((FrameworkElement) element).Height + num6))));
                point = rect.BottomRight;
                num1 = point.Y;
              }
              rect = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(new Rect(new Point(num5, num6), new Point(((FrameworkElement) element).Width + num5, ((FrameworkElement) element).Height + num6))));
              point = rect.TopLeft;
              if (point.X < num4)
              {
                rect = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(new Rect(new Point(num5, num6), new Point(((FrameworkElement) element).Width + num5, ((FrameworkElement) element).Height + num6))));
                point = rect.TopLeft;
                num4 = point.X;
              }
              rect = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(new Rect(new Point(num5, num6), new Point(((FrameworkElement) element).Width + num5, ((FrameworkElement) element).Height + num6))));
              point = rect.TopLeft;
              if (point.Y < num3)
              {
                rect = generalTransform.TransformBounds(element.TransformToVisual((Visual) canvas).TransformBounds(new Rect(new Point(num5, num6), new Point(((FrameworkElement) element).Width + num5, ((FrameworkElement) element).Height + num6))));
                point = rect.TopLeft;
                num3 = point.Y;
              }
            }
          }
        }
        if (num2 > num4 && num1 > num3)
        {
          list.Add(num2);
          list.Add(num1);
          list.Add(num4);
          list.Add(num3);
        }
      }
      return list;
    }
  }
}
